﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;
using System.Data.SqlClient;

namespace yfxt
{

    public partial class xxwh_rkfs : Form
    {
        string sqlstr, constr;
        SqlDataAdapter da;
        SqlCommandBuilder scb;
        DataTable dt = new DataTable();
        SqlConnection con;
        public xxwh_rkfs()
        {
            InitializeComponent();
        }


        private void xxwh_rkfs_Load_1(object sender, EventArgs e)
        {
            constr = "Data Source=Localhost;database=yf;uid=sa;pwd=123;timeout=60";
            sqlstr = "select rkfs,fsmc,rkdh,pydm,yfsb,xgpb from yf_rkfs where yfsb='" + mainform.yfsb + "'";
            con = new SqlConnection(constr);
            con.Open();
            da = new SqlDataAdapter(sqlstr, con);
            scb = new SqlCommandBuilder(da);
            try
            {
                da.Fill(dt);
                dataGridView1.Columns[0].DataPropertyName = dt.Columns[0].ToString();
                dataGridView1.Columns[1].DataPropertyName = dt.Columns[1].ToString();
                dataGridView1.Columns[2].DataPropertyName = dt.Columns[2].ToString();
                dataGridView1.Columns[3].DataPropertyName = dt.Columns[3].ToString();
                dataGridView1.Columns[4].DataPropertyName = dt.Columns[4].ToString();
                dataGridView1.Columns[5].DataPropertyName = dt.Columns[5].ToString();
                dataGridView1.DataSource = dt;
            }
            catch (Exception f)
            {
                MessageBox.Show(f.ToString());
            }
        }

        #region 新增
        private void button1_Click_1(object sender, EventArgs e)
        {
            DataRow dr;
            decimal maxckfs;
            string sqlselect;
            sqlselect = "select max(rkfs) from yf_rkfs";
            maxckfs = decimal.Parse(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlselect, null).ToString());

            try
            {
                dr = dt.NewRow();
                dr["rkfs"] = maxckfs + 1;
                dr["fsmc"] = null;
                dr["rkdh"] = 100001;
                dr["pydm"] = null;
                dr["xgpb"] = 1;
                dr["yfsb"] = mainform.yfsb;
                dt.Rows.Add(dr);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

        #region 删除
        private void button2_Click_1(object sender, EventArgs e)
        {
            int rowindex;
            rowindex = dataGridView1.CurrentRow.Index;
            dataGridView1.Rows.RemoveAt(rowindex);
            try
            {
                da.Update(dt);
                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
       #endregion

        #region 保存
        private void button3_Click_1(object sender, EventArgs e)
        {
            try
            {
                da.Update(dt);
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
       #endregion

        #region
        private void button4_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
